package linuxbox.resource.logic;

import java.util.*;

import linuxbox.bean.*;
import linuxbox.util.*;

import linuxbox.resource.bean.*;

public class BoxResource
{
  protected static BoxResource staticInstance = null;

   public static BoxResource getInstance()
   {
     if (staticInstance == null)
     {
       staticInstance = new BoxResource();
     }
     return staticInstance;
   }

   private BoxResource()
   {}

  public BoxResourceEntry get(BaseIntKey key)
  {
    BoxResourceEntry entry = null;
    try
    {
	    BoxResourceEntryBean bean = new BoxResourceEntryBean(key);
	    bean.load();
	    entry = new BoxResourceEntry();
	    entry.copyFrom(bean);
	  }
    catch (Exception e)
    {
      //News: Log something?
  	  e.printStackTrace();
    }
	  return entry;
  }

  public BoxResourceEntry add()
  {
    try
    {
      BoxResourceEntryBean bean = new BoxResourceEntryBean(new BaseIntKey(0));
		  bean.create();
      BoxResourceEntry entry = new BoxResourceEntry();
      entry.copyFrom(bean);
      return entry;
    }
    catch (Exception e)
    {
      //TODO: Log something?
  	  e.printStackTrace();
    }
    return null;
  }

  public boolean remove(BaseIntKey key)
  {
    try
    {
			BoxResourceEntryBean bean = new BoxResourceEntryBean(key);
  		bean.remove();

  		//TODO: clear the reservation
    }
    catch (Exception e)
    {
      //TODO: Log something?
  	  e.printStackTrace();
      return false;
    }
    return true;
  }

  public boolean update(BoxResourceEntry entry)
  {
    try
    {
      BoxResourceEntryBean bean = new BoxResourceEntryBean(new BaseIntKey(entry.getID()));
      bean.copyFrom(entry);
//System.out.println(bean);
      bean.store();
    }
    catch (Exception e)
    {
      //TODO: Log something?
  	  e.printStackTrace();
      return false;
    }
    return true;
  }

  public int getResourceCountByCat(int CatID)
  {
    int count = 0;
    try
    {
      BoxResourceEntryBean bean = new BoxResourceEntryBean(null);
      String sql = "select count(id) from "+bean.getTableName()
                  +" where category = " + CatID ;
      count = bean.getCount(sql);
    }
    catch (Exception e)
    {
  	  e.printStackTrace();
    //TODO: Log
    }
    return count;
  }

  public Iterator getResourceByCat(int CatID)
  {
    String sql = " where category = " + CatID ;
    return getBySql(sql);
  }

  public int getTotalPages(int entriesPerPage)
  {
    int pages = 1;
    try
    {
      BoxResourceEntryBean bean = new BoxResourceEntryBean(null,null);
      pages = ((bean.getCountBySql("") - 1) / entriesPerPage) + 1;
    }
    catch (Exception e)
    {
  	  e.printStackTrace();
    //TODO: Log
    }
    return pages;
  }

  public Iterator getResources(String SORTBY, boolean sortAsc, int from , int limit)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" order by ");
    sql.append(SORTBY);
    if (sortAsc)
      sql.append(" ASC ");
    else
      sql.append(" DESC ");

    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  protected Iterator getBySql(String sql)
  {
    Vector v = new Vector();
    try
    {
      // Use the transaction, pass the DbAdapter to the bean
  		DbAdapter db = new DbAdapter(true);
      BoxResourceEntryBean bean = new BoxResourceEntryBean(null,db);
      Iterator it = bean.findBySql(sql);
      while (it.hasNext())
      {
        BaseIntKey key = (BaseIntKey) it.next();
        bean = new BoxResourceEntryBean(key,db);
        bean.load();
        BoxResourceEntry entry = new BoxResourceEntry();
        entry.copyFrom(bean);
        v.add(entry);
      }
      db.close();
      db = null;
    }
    catch (Exception e)
    {
  	  e.printStackTrace();
    //TODO: Log
    }
    return v.iterator();
  }
}
